package com.lieve.bpm.config;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.impl.ProcessEngineImpl;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.activiti.engine.impl.interceptor.CommandInterceptor;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.io.InputStream;

/**
 * @author sunlijiang
 * @date 2020/5/1
 */
@Slf4j
public class LieveProcessEngineConfiguration extends ProcessEngineConfigurationImpl {

    private String resourceName;

    private SpringProcessEngineConfiguration springProcessEngineConfiguration;

    public LieveProcessEngineConfiguration(SpringProcessEngineConfiguration springProcessEngineConfiguration) {
        this.springProcessEngineConfiguration = springProcessEngineConfiguration;
    }

    @Override
    protected CommandInterceptor createTransactionInterceptor() {
        return null;
    }

    public void initFromProcessEngineConfiguration(ProcessEngineConfigurationImpl processEngineConfiguration, String resourceName) {
        this.resourceName = resourceName;
        setDatabaseType(processEngineConfiguration.getDatabaseType());
        setDataSource(processEngineConfiguration.getDataSource());
        setDatabaseTablePrefix(processEngineConfiguration.getDatabaseTablePrefix());

        initDataSource();
        initVariableTypes();
        initCommandContextFactory();
        initTransactionFactory();
        initTransactionContextFactory();
        initCommandExecutors();
        initSqlSessionFactory();
        initSessionFactories();
    }

    @Override
    protected InputStream getMyBatisXmlConfigurationSteam() {
        log.debug("log");
        return getResourceAsStream("com/lieve/bpm/mapper/mappings.xml");
    }

    @Override
    public ProcessEngine buildProcessEngine() {
        init();
        return new ProcessEngineImpl(this);
    }

}
